﻿
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using Utils;
using PanasonicWarranty.DAL;
namespace PanasonicWarranty.Business
{
    public partial class SysMenu
    {
    	#region public properties
		[Required(ErrorMessage="MenuID Required")]
		public int MenuID { get; set; }
		[Required(ErrorMessage="Menu Code Required")]
		public string MenuCode { get; set; }
		[Required(ErrorMessage="Menu Name Required")]
		public string MenuName { get; set; }
		public int? ParentID { get; set; }
		public string Level { get; set; }
		public string Link { get; set; }
		public string Target { get; set; }
		public string Image { get; set; }
		public string Description { get; set; }
		public int? Ord { get; set; }
		public bool? Disabled { get; set; }
		#endregion

		#region constructor
		public SysMenu(){ }

		public SysMenu(IDataReader rd)
		{
			this.MenuID = (int)rd["MenuID"];
			this.MenuCode = rd["MenuCode"].ToString();
			this.MenuName = rd["MenuName"].ToString();
			this.ParentID = rd["ParentID"].Equals(DBNull.Value) ? (int?)null : (int)rd["ParentID"];
			this.Level = rd["Level"].ToString();
			this.Link = rd["Link"].ToString();
			this.Target = rd["Target"].ToString();
			this.Image = rd["Image"].ToString();
			this.Description = rd["Description"].ToString();
			this.Ord = rd["Ord"].Equals(DBNull.Value) ? (int?)null : (int)rd["Ord"];
			this.Disabled = rd["Disabled"].Equals(DBNull.Value) ? (bool?)null : (bool)rd["Disabled"];
		}
		#endregion

		#region private method
        private bool Create()
        {
            return DBSysMenu.Create(MenuCode, MenuName, ParentID, Level, Link, Target, Image, Description, Ord, Disabled);
        }

        private bool Update()
        {
            return DBSysMenu.Update(MenuID, MenuCode, MenuName, ParentID, Level, Link, Target, Image, Description, Ord, Disabled);
        }
		#endregion

		#region public method
		public bool Save()
		{
            if(MenuID != 0)
            {
                return Update();
            }
            else
            {
                return Create();
            }
		}
		#endregion

		#region static method
        public static bool Delete(int MenuID)
        {
            return DBSysMenu.Delete(MenuID);
        }

        public static List<SysMenu> GetAll()
        {
            return ObjectClass.CreateList<SysMenu>(DBSysMenu.GetAll());
        }

        public static SysMenu GetByKey(int MenuID)
        {
            return ObjectClass.CreateInstance<SysMenu>(DBSysMenu.GetByKey(MenuID));
        }

        public static List<SysMenu> GetByTop(string top, string where, string order)
        {
            return ObjectClass.CreateList<SysMenu>(DBSysMenu.GetByTop(top, where, order));
        }
		#endregion
	}
}
